package com.itheima.bos.service.impl;

import com.itheima.bos.dao.IWorkordermanageDao;
import com.itheima.bos.domain.Workordermanage;
import com.itheima.bos.service.IWorkordermanageService;
import com.itheima.bos.utils.PageBean;
import org.activiti.engine.ProcessEngine;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Properties;

/**
 * Created by Mr.King on 2017/3/15.
 */
@Service
@Transactional
public class WorkordermanageServiceImpl implements IWorkordermanageService {

    @Autowired
    private IWorkordermanageDao workordermanageDao;

    @Override
    public void save(Workordermanage model) {
        workordermanageDao.save(model);
    }

    @Override
    public void pageQuery(PageBean pageBean) {
        workordermanageDao.pageQuery(pageBean);
    }

    @Override
    public List<Workordermanage> findListNotStart() {
        DetachedCriteria start = DetachedCriteria.forClass(Workordermanage.class)
                        .add(Restrictions.eq("start", "0"));
        return workordermanageDao.findCriteria(start);
    }

    //注入流程引擎对象
    @Autowired
    private ProcessEngine processEngine;

    @Override
    public void findById(String id) {
        Workordermanage workordermanage = workordermanageDao.findById(id);
        workordermanage.setStart("1");//设置为启动状态

        //启动流程实例，设置流程变量
        String key = "transfer";
        HashMap<String, Object> map = new HashMap<>();
        map.put("业务数据",workordermanage);

        processEngine.getRuntimeService().startProcessInstanceByKey(key,map);
    }

    @Override
    public void update(Workordermanage workordermanage) {
        workordermanageDao.saveOrUpdate(workordermanage);
    }
}
